/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.service.impl;

import com.rits.cloning.Cloner;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.apache.commons.lang.StringUtils;
import org.cx1.model.Invite;
import org.cx1.model.User;
import org.cx1.model.ds.MockDS;
import org.cx1.model.utils.EmailInvitee;
import org.cx1.model.utils.Invitee;
import org.cx1.model.utils.MobileInvitee;
import org.cx1.model.utils.ServiceResponse;
import org.cx1.service.api.IEmailService;
import org.cx1.service.api.IInviteService;
import org.cx1.service.api.IUserService;

/**
 *
 * @author ECH
 */
public class MInviteService implements IInviteService {

    private IEmailService emailService = new MEmailService();
    private IUserService userService = new MUserService();
    private Cloner cloner = new Cloner();
    
    @Override
    public ServiceResponse<String> create(String name, String email, String type, String area) {
        ServiceResponse<String> response = new ServiceResponse<String>();
        String id = null;        
        try {            
            Invite entry = new Invite();
            id = UUID.randomUUID().toString();
            
            entry.setId(id);      
            
            entry.setName(name);
            entry.setEmail(email);
            entry.setType(type);
            entry.setArea(area);
                        
            entry.setActive(true);
            entry.setDateCreated(new Date());
            entry.setDateModified(new Date());
                        
            MockDS.getInvites().add(entry);
            
            emailService.sendInviteConfirmation(entry);
            
            response.setResponse(id);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<List<Invite>> readll(int offset, int limit) {
        ServiceResponse<List<Invite>> response = new ServiceResponse<List<Invite>>();
        List<Invite> inquiries = new ArrayList<Invite>();
        try {            
            List<Invite> records = MockDS.getInvites();
            if (records != null && records.size() > 0) {
                if (offset == 0 && limit == 0) {
                    inquiries.addAll(records);
                }
                else if (offset < records.size() && limit < records.size()) {
                    inquiries.addAll(records);
                }              
            }            
                      
            response.setResponse(inquiries);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<List<Invitee>> readAllInvitee(int offset, int limit) {
        ServiceResponse<List<Invitee>> response = new ServiceResponse<List<Invitee>>();
        List<Invitee> invitees = new ArrayList<Invitee>();
        try {            
            List<Invitee> records = MockDS.getInvitees();
            if (records != null && records.size() > 0) {
                if (offset == 0 && limit == 0) {
                    invitees.addAll(records);
                }
                else if (offset < records.size() && limit < records.size()) {
                    invitees.addAll( records.subList(offset, limit) );
                }              
            }            
            response.setResponse(invitees);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<String> createInvitee(Invitee invitee) {
        ServiceResponse<String> response = new ServiceResponse<String>();
        String id = null;        
        try {  
            Invitee entry = new Invitee();
            id = UUID.randomUUID().toString();

            entry.setId(id);
            entry.setName(invitee.getName());
            entry.setEmail(invitee.getEmail());
            entry.setMobile(invitee.getMobile());
               
            entry.setActive(true);
            entry.setDateCreated(new Date());
            entry.setDateModified(new Date());

            MockDS.getInvitees().add(entry);
            
            response.setResponse(id);     
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<Boolean> deleteInvitee(String id) {
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {            
            List<Invitee> records = MockDS.getInvitees();
            if (records != null && records.size() > 0) {
                
                for (int i = 0; i < records.size(); i++) {
                    Invitee record = records.get(i);
                    
                    if (StringUtils.equalsIgnoreCase(record.getId(), id)) {
                        records.remove(i);
                        success = true;
                        break;
                    }
                }
            }            
            response.setResponse(success);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<List<EmailInvitee>> readAllEmailInvitee(int offset, int limit) {
        ServiceResponse<List<EmailInvitee>> response = new ServiceResponse<List<EmailInvitee>>();
        List<EmailInvitee> emailInvitees = new ArrayList<EmailInvitee>();
        try {            
            Set<EmailInvitee> records = MockDS.getEmailInvitees();
            if (records != null && records.size() > 0) {
                if (offset == 0 && limit == 0) {
                    emailInvitees.addAll(records);
                }            
            }            
            response.setResponse(emailInvitees);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<String> createEmailInvitee(EmailInvitee emailInvitee) {
        ServiceResponse<String> response = new ServiceResponse<String>();
        String id = null;        
        try {  
            if (!StringUtils.isEmpty(emailInvitee.getEmail())) {
                EmailInvitee entry = new EmailInvitee();
                id = UUID.randomUUID().toString();

                entry.setId(id);
                entry.setName(emailInvitee.getName());
                entry.setEmail(emailInvitee.getEmail().trim());

                entry.setActive(true);
                entry.setDateCreated(new Date());
                entry.setDateModified(new Date());

                MockDS.getEmailInvitees().add(entry);

                response.setResponse(id);     
                response.setSuccessful(true); 
            }      
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<Boolean> deleteEmailInvitee(String id) {
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {            
            List<EmailInvitee> records = new ArrayList<EmailInvitee>(MockDS.getEmailInvitees());            
            if (records != null && records.size() > 0) {
                
                for (int i = 0; i < records.size(); i++) {
                    EmailInvitee record = records.get(i);
                    
                    if (StringUtils.equalsIgnoreCase(record.getId(), id)) {
                        records.remove(i);
                        MockDS.getEmailInvitees().remove(record);
                        success = true;
                        break;
                    }
                }
            }            
            response.setResponse(success);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<List<EmailInvitee>> readEmailInviteesByProcessed(int offset, int limit, boolean processed) {
        ServiceResponse<List<EmailInvitee>> response = new ServiceResponse<List<EmailInvitee>>();
        List<EmailInvitee> emailInvitees = new ArrayList<EmailInvitee>();
        try {            
            Set<EmailInvitee> records = MockDS.getEmailInvitees();
            Set<EmailInvitee> processedRecords = new HashSet();
         
            if (records != null && records.size() > 0) {
                for (EmailInvitee invitee : records) {
                    if (invitee.isProcessed() == processed) {
                        processedRecords.add(invitee);
                    }
                }                
            }
            
            if (processedRecords != null && processedRecords.size() > 0) {
                if (offset == 0 && limit == 0) {
                    emailInvitees.addAll(processedRecords);
                }            
                else {
                    emailInvitees = new ArrayList(processedRecords);
                    if (emailInvitees.size() < limit) {    
                        //CX: do nothing
                    }
                    else {
                        emailInvitees = emailInvitees.subList(offset, limit);                        
                    }
                }
            }            
            response.setResponse(emailInvitees);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<Boolean> setEmailInviteeProcessed(String id, boolean processed) {
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {            
            Set<EmailInvitee> records = MockDS.getEmailInvitees();
            if (records != null && records.size() > 0) {                
                for (EmailInvitee emailInvitee : records) {
                    if (StringUtils.equalsIgnoreCase(emailInvitee.getId(), id)) {
                        emailInvitee.setProcessed(processed);
                        break;
                    }
                }
            }            
            success = true;
            response.setResponse(success);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<Boolean> deleteEmailInviteeByEmail(String email) {
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {            
            List<EmailInvitee> records = new ArrayList<EmailInvitee>(MockDS.getEmailInvitees());            
            if (records != null && records.size() > 0) {
                
                for (int i = 0; i < records.size(); i++) {
                    EmailInvitee record = records.get(i);
                    
                    if (StringUtils.equalsIgnoreCase(record.getEmail(), email)) {
                        records.remove(i);
                        MockDS.getEmailInvitees().remove(record);
                        success = true;
                        System.out.println("CX: deleted email invitee- " + email);
                        break;
                    }
                }
            }            
            response.setResponse(success);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<Boolean> deleteExisitingUsersInEmailInviteList() {
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {            
            ServiceResponse<List<User>> response1 = userService.readAll(0, 0);            
            if (response1.isSuccessful() && response1.getResponse() != null && response1.getResponse().size() > 0) {
                for (User user : response1.getResponse()) {
                    this.deleteEmailInviteeByEmail(user.getEmail());
                }
            }
            success = true;
            response.setResponse(success);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }
    
    
    @Override
    public ServiceResponse<Boolean> deleteDuplicatesInEmailInviteList() {
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {            
            
            Set<EmailInvitee> emailInvitees = MockDS.getEmailInvitees();
            Set<EmailInvitee> uqSet = new HashSet<EmailInvitee>();
            
            for (EmailInvitee emailInvitee : emailInvitees) {
                EmailInvitee uqInvitee = cloner.deepClone(emailInvitee);
                if (!StringUtils.isEmpty(uqInvitee.getEmail())) {
                    uqInvitee.setEmail( uqInvitee.getEmail().trim() );
                    uqSet.add(uqInvitee);
                }
            }
            
            MockDS.getEmailInvitees().clear();
            MockDS.getEmailInvitees().addAll(uqSet);
            
            success = true;
            response.setResponse(success);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<List<MobileInvitee>> readAllMobileInvitee(int offset, int limit) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ServiceResponse<String> createMobileInvitee(MobileInvitee mobileInvitee) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ServiceResponse<Boolean> deleteMobileInvitee(String id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ServiceResponse<Boolean> deleteMobileInviteeByMobile(String id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ServiceResponse<List<MobileInvitee>> readMobileInviteesByProcessed(int offset, int limit, boolean processed) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ServiceResponse<Boolean> setMobileInviteeProcessed(String id, boolean processed) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}
